package com.joysuch.wwyt.workflow.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.workflow.entity.WorkflowWatermarkItem;
import com.joysuch.wwyt.workflow.entity.WorkflowWatermarkSettings;
import com.joysuch.wwyt.workflow.entity.dto.WorkflowWatermarkItemDto;
import com.joysuch.wwyt.workflow.entity.dto.WorkflowWatermarkSettingsDto;
import com.joysuch.wwyt.workflow.entity.vo.WorkflowWatermarksettingsVO;
import com.joysuch.wwyt.workflow.repository.WorkflowWatermarkItemDao;
import com.joysuch.wwyt.workflow.repository.WorkflowWatermarkSettingsDao;
import com.joysuch.wwyt.workflow.service.WorkflowWatermarkSettingsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zero
 * @since 2021-10-25
 */
@Service
public class WorkflowWatermarkSettingsServiceImpl implements WorkflowWatermarkSettingsService {

    @Autowired
    private WorkflowWatermarkSettingsDao workflowWatermarksettingsDao;
    @Autowired
    private WorkflowWatermarkItemDao workflowWatermarkItemDao;
    
    @Override
    public WorkflowWatermarkSettingsDto getWatermarkSettingByProcessId(Long workflowId) {

        WorkflowWatermarkSettingsDto workflowWatermarksettingsDto = null;
        WorkflowWatermarkSettings workflowWatermarksetting = workflowWatermarksettingsDao.findByWorkflowId(workflowId);
        //配置为空，直接返回结果
        if (workflowWatermarksetting != null){
            //找到配置对应的图片和选项,使用dto返回给前端
            List<WorkflowWatermarkItem> workflowWatermarkItems = workflowWatermarkItemDao.findAllByWatermarkId(workflowWatermarksetting.getId());
            workflowWatermarksettingsDto = new WorkflowWatermarkSettingsDto();
            BeanUtils.copyProperties(workflowWatermarksetting,workflowWatermarksettingsDto);
            List<WorkflowWatermarkItemDto> watermarkItemTfList = new ArrayList<>();
            for (WorkflowWatermarkItem watermarkItem : workflowWatermarkItems) {
                WorkflowWatermarkItemDto workflowWatermarkItemDto = new WorkflowWatermarkItemDto();
                BeanUtils.copyProperties(watermarkItem,workflowWatermarkItemDto);
                watermarkItemTfList.add(workflowWatermarkItemDto);
            }
            workflowWatermarksettingsDto.setWorkflowWatermarkItemDtos(watermarkItemTfList);
        }
        return workflowWatermarksettingsDto;
        
    }

    
    @Override
    @Transactional(rollbackOn = Exception.class)
    public void addWatermarkSetting(WorkflowWatermarksettingsVO workflowWatermarksettingsVO) {
        //第一次新增则没有id直接创建对象将vo对象copy给它
        WorkflowWatermarkSettings workflowWatermarksettings = workflowWatermarksettingsVO.getId() == null ? 
                new WorkflowWatermarkSettings() : workflowWatermarksettingsDao.findById(workflowWatermarksettingsVO.getId()).orElseGet(WorkflowWatermarkSettings::new);
        //修改直接删除之前的图片数据然后重新增加
        if (workflowWatermarksettingsVO.getId() != null){
            workflowWatermarkItemDao.deleteByWatermarkId(workflowWatermarksettingsVO.getId());
        }
        BeanUtils.copyProperties(workflowWatermarksettingsVO,workflowWatermarksettings);
        workflowWatermarksettingsDao.save(workflowWatermarksettings);
        List<WorkflowWatermarkItem> workflowWatermarkItems = workflowWatermarksettingsVO.getWorkflowWatermarkItems();
        //如果图片不为空则新增
        if (CollectionUtils.isNotEmpty(workflowWatermarkItems)){
            for (WorkflowWatermarkItem workflowWatermarkItem : workflowWatermarkItems) {
                workflowWatermarkItem.setWatermarkId(workflowWatermarksettings.getId());
            }
            workflowWatermarkItemDao.saveAll(workflowWatermarkItems);
        }
    }
}
